Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [], // 文件列表
    selectedFiles: [], // 已选择的文件
    fileTypes: ['全部类型', 'PDF文档', 'Word文档', 'Excel表格', '图片文件'],
    fileTypeIndex: 0,
    sortOptions: ['按名称排序', '按时间排序', '按大小排序'],
    sortIndex: 0,
    showPrintModal: false,
    printCopies: 1,
    doubleSided: true,
    totalPages: 0,
    originalFiles: [] // 保存原始文件列表以便筛选
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 初始化文件列表
    this.initFileList();
  },

  /**
   * 初始化文件列表
   */
  initFileList: function() {
    // 模拟文件数据
    const mockFiles = [
      {
        id: 1,
        name: '销售合同-2025.pdf',
        size: '2.3MB',
        sizeInKB: 2358, // 添加KB大小用于排序
        date: '2025-01-15',
        path: '/documents/sales/',
        type: 'pdf',
        typeIcon: '📄',
        pages: 8,
        selected: false
      },
      {
        id: 2,
        name: '财务报表.xlsx',
        size: '856KB',
        sizeInKB: 856,
        date: '2025-01-14',
        path: '/documents/finance/',
        type: 'excel',
        typeIcon: '📊',
        pages: 3,
        selected: false
      },
      {
        id: 3,
        name: '项目计划书.docx',
        size: '1.2MB',
        sizeInKB: 1228, // 约1.2MB
        date: '2025-01-13',
        path: '/documents/project/',
        type: 'word',
        typeIcon: '📘',
        pages: 12,
        selected: false
      },
      {
        id: 4,
        name: '产品介绍.pdf',
        size: '3.8MB',
        sizeInKB: 3891,
        date: '2025-01-12',
        path: '/documents/product/',
        type: 'pdf',
        typeIcon: '📄',
        pages: 6,
        selected: false
      },
      {
        id: 5,
        name: '会议纪要.docx',
        size: '634KB',
        sizeInKB: 634,
        date: '2025-01-11',
        path: '/documents/meeting/',
        type: 'word',
        typeIcon: '📘',
        pages: 4,
        selected: false
      },
      {
        id: 6,
        name: '流程图.png',
        size: '892KB',
        sizeInKB: 892,
        date: '2025-01-10',
        path: '/documents/images/',
        type: 'image',
        typeIcon: '🖼️',
        pages: 1,
        selected: false
      }
    ];

    this.setData({
      fileList: mockFiles,
      originalFiles: mockFiles // 保存原始文件列表以便筛选
    });
  },

  /**
   * 文件类型筛选改变
   */
  onFileTypeChange: function(e) {
    const index = parseInt(e.detail.value);
    this.setData({
      fileTypeIndex: index
    });
    this.filterFiles();
  },

  /**
   * 排序方式改变
   */
  onSortChange: function(e) {
    const index = parseInt(e.detail.value);
    this.setData({
      sortIndex: index
    });
    this.sortFiles();
  },

  /**
   * 筛选文件
   */
  filterFiles: function() {
    const selectedType = this.data.fileTypes[this.data.fileTypeIndex];
    const originalFiles = this.data.originalFiles || [...this.data.fileList];
    let filteredFiles = [];
    
    // 根据文件类型进行筛选
    if (selectedType === '全部类型') {
      filteredFiles = [...originalFiles];
    } else {
      // 根据文件类型进行匹配
      const typeMapping = {
        'PDF文档': 'pdf',
        'Word文档': 'word',
        'Excel表格': 'excel',
        '图片文件': 'image'
      };
      
      const filterType = typeMapping[selectedType];
      filteredFiles = originalFiles.filter(file => file.type === filterType);
    }
    
    // 保持当前的排序方式
    this.setData({
      fileList: filteredFiles
    }, () => {
      // 筛选后应用当前的排序方式
      this.sortFiles();
    });
  },

  /**
   * 排序文件
   */
  sortFiles: function() {
    const sortOption = this.data.sortOptions[this.data.sortIndex];
    const files = [...this.data.fileList];
    
    switch (sortOption) {
      case '按名称排序':
        files.sort((a, b) => a.name.localeCompare(b.name));
        break;
        
      case '按时间排序':
        files.sort((a, b) => new Date(b.date) - new Date(a.date)); // 按日期降序排列
        break;
        
      case '按大小排序':
        files.sort((a, b) => b.sizeInKB - a.sizeInKB); // 按大小降序排列
        break;
        
      default:
        break;
    }
    
    this.setData({
      fileList: files
    });
  },

  /**
   * 切换文件选择状态
   */
  toggleFileSelection: function(e) {
    const index = e.currentTarget.dataset.index;
    const fileList = this.data.fileList;
    const selectedFiles = this.data.selectedFiles;

    // 切换选择状态
    fileList[index].selected = !fileList[index].selected;

    // 更新已选择的文件列表
    let newSelectedFiles = [];
    let totalPages = 0;
    fileList.forEach(file => {
      if (file.selected) {
        newSelectedFiles.push(file);
        totalPages += file.pages;
      }
    });

    this.setData({
      fileList: fileList,
      selectedFiles: newSelectedFiles,
      totalPages: totalPages
    });
  },

  /**
   * 全选
   */
  selectAll: function() {
    const fileList = this.data.fileList;
    let totalPages = 0;

    fileList.forEach(file => {
      file.selected = true;
      totalPages += file.pages;
    });

    this.setData({
      fileList: fileList,
      selectedFiles: [...fileList],
      totalPages: totalPages
    });

    wx.showToast({
      title: '已全选所有文件',
      icon: 'success',
      duration: 1500
    });
  },

  /**
   * 清空选择
   */
  clearSelection: function() {
    const fileList = this.data.fileList;
    fileList.forEach(file => {
      file.selected = false;
    });

    this.setData({
      fileList: fileList,
      selectedFiles: [],
      totalPages: 0
    });

    wx.showToast({
      title: '已清空选择',
      icon: 'none',
      duration: 1500
    });
  },

  /**
   * 开始批量打印
   */
  startBatchPrint: function() {
    if (this.data.selectedFiles.length === 0) {
      wx.showToast({
        title: '请先选择要打印的文件',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    this.setData({
      showPrintModal: true
    });
  },

  /**
   * 关闭打印确认弹窗
   */
  closePrintModal: function() {
    this.setData({
      showPrintModal: false
    });
  },

  /**
   * 增加打印份数
   */
  increaseCopies: function() {
    this.setData({
      printCopies: this.data.printCopies + 1
    });
  },

  /**
   * 减少打印份数
   */
  decreaseCopies: function() {
    if (this.data.printCopies > 1) {
      this.setData({
        printCopies: this.data.printCopies - 1
      });
    }
  },

  /**
   * 双面打印开关改变
   */
  onDoubleSidedChange: function(e) {
    this.setData({
      doubleSided: e.detail.value
    });
  },

  /**
   * 确认打印
   */
  confirmPrint: function() {
    wx.showLoading({
      title: '正在打印...'
    });

    // 模拟打印过程
    setTimeout(() => {
      wx.hideLoading();
      
      wx.showModal({
        title: '打印完成',
        content: `已成功打印 ${this.data.selectedFiles.length} 个文件，共 ${this.data.totalPages} 页`,
        showCancel: false,
        confirmText: '确定',
        success: () => {
          // 返回上一页
          wx.navigateBack();
        }
      });
    }, 2000);

    this.setData({
      showPrintModal: false
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  }
}); 